<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button size="mini" type="primary" @click="showDialog = true">
          添加角色
        </el-button>
      </div>

      <!-- table 组件 -->
      <el-table :data="list">
        <el-table-column prop="name" width="200px" align="center" label="角色">
          <template v-slot="{row}">
            <!-- 编辑 -->
            <el-input v-if="row.isEdit" v-model="row.editContent.name" size="mini" />
            <!-- 非编辑 -->
            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="state" width="200px" align="center" label="启用">
          <template v-slot="{row}">
            <template>
              <!-- 开1关0 -->
              <!-- 编辑 -->
              <el-switch v-if="row.isEdit" v-model="row.editContent.state" size="mini" :active-value="1" :inactive-value="0" />
              <!-- 非编辑 -->
              <span v-else>
                {{ row.state === 1 ? '已启用' : row.state === 0 ? '未启用' : '无' }}
              </span>
            </template>
          </template>
        </el-table-column>
        <el-table-column prop="description" align="center" label="描述">
          <template v-slot="{ row }">
            <!-- 编辑状态 -->
            <el-input v-if="row.isEdit" v-model="row.editContent.description" type="textarea" />
            <!-- 非编辑状态 -->
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template v-slot="{ row }">
            <template v-if="row.isEdit">
              <!-- 编辑状态 -->
              <el-button type="primary" size="mini" @click="btnRowEdit(row)">
                确定
              </el-button>
              <el-button size="mini" @click="row.isEdit = false">
                取消
              </el-button>
            </template>
            <template v-else>
              <!-- 非编辑状态 -->
              <el-button type="text" size="small ">
                分配权限
              </el-button>
              <el-button type="text" size="small " @click="rowEdit(row)">
                编辑
              </el-button>
              <el-button type="text" size="small " @click="delRole(row.id)">
                删除
              </el-button>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="end" align="middle" style="height: 60px;">
        <!-- 分页组件 -->
        <el-pagination
          :page-size="pageParams.pagesize"
          :current-page="pageParams.page"
          :total="pageParams.total"
          layout="prev, pager, next"
          @current-change="changePage"
        />
      </el-row>
    </div>
    <!-- 新增角色对话框 -->
    <el-dialog
      title="新增角色"
      :visible.sync="showDialog"
      width="500px"
      @close="btnCancel"
    >
      <el-form ref="form" label-width="120px" :model="addRoleForm" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="addRoleForm.name" size="mini" style="width: 300px;" />
        </el-form-item>
        <el-form-item label="启用">
          <el-switch v-model="addRoleForm.state" size="mini" :active-value="1" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="addRoleForm.description" type="textarea" :rows="3" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="18">
              <el-button type="primary" size="mini" @click="btnOK">确定</el-button>
              <el-button size="mini" @click="btnCancel">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getRoleList, addRole, updateRole, delRole } from '@/api/role'
export default {
  name: 'Role',
  data() {
    return {
      list: [],
      // 分页参数设置
      pageParams: {
        page: 1, // 第几页
        pagesize: 5, // 每页多少条
        total: 0
      },
      showDialog: false,
      addRoleForm: {
        name: '',
        description: '',
        state: 0
      },
      rules: {
        name: [{
          required: true,
          trigger: 'blur',
          message: '请输入角色名称'
        }],
        description: [{
          required: true,
          trigger: 'blur',
          message: '请输入角色描述'
        }]
      }
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 获得角色数据
    async getRoleList() {
      const { rows, total } = await getRoleList(this.pageParams)
      // console.log(rows)
      this.list = rows
      this.pageParams.total = total
      // 针对每一行添加可编辑的标记
      // this.$set(目标对象, 属性名称, 初始值) 可以针对目标对象 添加的属性 添加响应式
      this.list.forEach(item => {
        this.$set(item, 'isEdit', false)
        // 缓存数据
        this.$set(item, 'editContent', {
          name: item.name,
          state: item.state,
          description: item.description
        })
      })
    },
    // 改变页码时，同时改变显示内容
    changePage(newPage) {
      // 赋值当前页码
      this.pageParams.page = newPage
      // 重新获取当前页码所对应的数据
      this.getRoleList()
    },
    btnOK() {
      this.$refs.form.validate(async(isOk) => {
        if (isOk) {
          await addRole(this.addRoleForm)
          this.$message.success('新增角色成功')
          this.getRoleList()
          this.btnCancel()
        }
      })
    },
    btnCancel() {
      this.$refs.form.resetFields()
      this.showDialog = false
    },
    // 行内编辑
    rowEdit(row) {
      // 打开编辑模式
      row.isEdit = true
      // 更新缓存数据
      row.editContent.name = row.name
      row.editContent.state = row.state
      row.editContent.description = row.description
    },
    async btnRowEdit(row) {
      // console.log(row)
      if (row.editContent.name && row.editContent.description) {
        // 不为空
        // 更新
        await updateRole({ ...row.editContent, id: row.id })
        this.$message.success('更新成功')
        Object.assign(row, {
          ...row.editContent,
          isEdit: false
        })
      } else {
        this.$message.warning('名称和描述不能为空')
      }
    },
    async delRole(id) {
      await delRole(id)
      this.$message.success('删除角色成功')
      // 删除的如果是最后一个
      if (this.list.length === 1) this.pageParams.page--
      this.getRoleList()
    }
  }
}
</script>
